package org.link.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import org.link.bean.Board;
import org.link.util.DBUtil;
import org.link.util.DateUtil;

/**
 * 
 * project_name:forumweb
 * package_name:org.link.dao
 * file_name:BoardDao.java
 * @author Link
 * create_date:2015年10月23日 下午12:38:08
 * modified_date:
 * description: 板块导方法类
 *
 */
public class BoardDao {

	/**
	 * 
	 * description: 判断板块名是否存在
	 * 2015年10月23日 下午12:39:53
	 * @author Link
	 * @param name
	 * @return
	 *
	 */
	@SuppressWarnings("resource")
	public boolean checkBoardExist(String name){
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select board_name from t_board where board_name=?");
			pstmt.setString(1, name);
			rs= pstmt.executeQuery();
			while(rs.next()){
				if(rs.getString("board_name").equals(name)){
					return true;//true表示已存在
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return false;//false表示不存在
	}
	
	/**
	 * 
	 * description:添加父板块
	 * 2015年10月23日 下午5:30:01
	 * @author Link
	 * @param name
	 * @param desc
	 * @return
	 *
	 */
	public int addFatherBoard(String name,String desc){
		int result =0;
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("insert into t_board(board_name,board_desc,addtime)values(?,?,?)");
			pstmt.setString(1, name);
			pstmt.setString(2, desc);
			pstmt.setString(3, DateUtil.getDate(new Date()));
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn);
		}
		return result;
	}
	
	/**
	 * 
	 * description: 查询并返回所有的板块信息
	 * 2015年10月23日 下午5:37:43
	 * @author Link
	 * @return
	 *
	 */
	public ArrayList<Board> queryAllBoard(){
		ArrayList<Board> boards = new ArrayList<Board>();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select id,board_name,board_desc,addtime,pid from t_board");
			rs= pstmt.executeQuery();
			while (rs.next()){
				Board board = new Board();
				board.setAddTime(rs.getString("addtime"));
				board.setDesc(rs.getString("board_desc"));
				board.setId(rs.getInt("id"));
				board.setName(rs.getString("board_name"));
				board.setPid(rs.getInt("pid"));
				boards.add(board);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return boards;
	}
	
	/**
	 * 
	 * description: 查询并返回所有父板块
	 * creatTime:2015年10月23日下午11:09:49
	 * @author Link
	 * return type: ArrayList<String>
	 *
	 */
	public ArrayList<String> queryAllFatherBoard(){
		ArrayList<String> allFatherBoard = new ArrayList<String>();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select board_name from t_board where pid=0");
			rs = pstmt.executeQuery();
			while(rs.next()){
				allFatherBoard.add(rs.getString("board_name"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return allFatherBoard;
	}
	
	/**
	 * 
	 * description:查询并返回所以父板块信息
	 * 2015年10月27日 下午3:20:41
	 * @author Link
	 * @return
	 *
	 */
	public ArrayList<Board> queryFatherBoard(){
		ArrayList<Board> allFatherBoard = new ArrayList<Board>();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select board_name,board_desc,addtime,id from t_board where pid=0");
			rs = pstmt.executeQuery();
			while(rs.next()){
				Board board = new Board();
				board.setAddTime(rs.getString("addtime"));
				board.setDesc(rs.getString("board_desc"));
				board.setName(rs.getString("board_name"));
				board.setId(rs.getInt("id"));
				board.setPid(0);
				allFatherBoard.add(board);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return allFatherBoard;
	}
	
	/**
	 * 
	 * description:根据板块名称获取板块id号
	 * creatTime:2015年10月23日下午11:52:40
	 * @author Link
	 * return type: int
	 *
	 */
	public int queryFatherBoardId(String boardName){
		int result = -1;
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select id from t_board where board_name=?");
			pstmt.setString(1, boardName);
			rs = pstmt.executeQuery();
			while(rs.next()){
				result = rs.getInt("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return result;
	}
	
	/**
	 * 
	 * description: 根据传入的父id号查找所有的子板块名字并返回
	 * 2015年10月26日 下午12:48:18
	 * @author Link
	 * @param pid
	 * @return
	 *
	 */
	public ArrayList<String> queryChildName(int pid){
		
		ArrayList<String> childName = new ArrayList<String>();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select board_name,board_desc,addtime,id from t_board where pid = ?");
			pstmt.setInt(1, pid);
			rs = pstmt.executeQuery();
			while(rs.next()){
				childName.add(rs.getString("board_name"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return childName;
	}
	
	/**
	 * 
	 * description: 根据输入的父板块id号查询所有的子版块信息
	 * 2015年10月27日 下午12:36:56
	 * @author Link
	 * @param pid
	 * @return
	 *
	 */
	public ArrayList<Board> queryChildBoard(int pid){
		ArrayList<Board> childBoard = new ArrayList<Board>();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select board_name,board_desc,addtime,id from t_board where pid = ?");
			pstmt.setInt(1, pid);
			rs = pstmt.executeQuery();
			while(rs.next()){
				Board board = new Board();
				board.setAddTime(rs.getString("addtime"));
				board.setDesc(rs.getString("board_desc"));
				board.setName(rs.getString("board_name"));
				board.setId(rs.getInt("id"));
				board.setPid(pid);
				childBoard.add(board);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return childBoard;
	}

	/**
	 * 
	 * description: 添加子版块
	 * creatTime:2015年10月24日上午12:10:51
	 * @author Link
	 * return type: int
	 *
	 */
	public int addChildBoard(int fatherId,String childName,String childDesc){
		int result = -1;
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("insert into t_board(board_name,board_desc,addtime,pid)values(?,?,?,?)");
			pstmt.setString(1, childName);
			pstmt.setString(2, childDesc);
			pstmt.setString(3, DateUtil.getDate(new Date()));
			pstmt.setInt(4, fatherId);
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn);
		}
		return result;
	}
	
	/**
	 * 
	 * description:根据传入的boardName查找对应的id号
	 * 2015年10月27日 下午5:58:29
	 * @author Link
	 * @param name
	 * @return
	 *
	 */
	public int queryBoardId(String name){
		int result = 0;
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select id from t_board where board_name=?");
			pstmt.setString(1, name);
			rs = pstmt.executeQuery();
			while(rs.next()){
				result = rs.getInt("id");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return result;
	}
	
	
}








